package com.ssharing.controller;

import com.ssharing.entities.User;
import com.ssharing.entities.User.UserBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController()
@RequestMapping("/user")
public class UserController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<User> getUserList() {
        return getUsers();
    }

    @RequestMapping("/{userId}")
    public User getUserById(@PathVariable String userId) {
        Stream<User> userStream = getUsers().stream().filter(x -> x.getUserId().equals((userId)));
        User user = userStream.findFirst().get();
        String url = "http://order-service/order/list/user/" + user.getUserId();
        String obj = restTemplate.getForObject(url, String.class);
        user.setOrderJson(obj);
        return user;
    }

    private List<User> getUsers() {
        User u1 = User.builder().userId("1").age(18).mobile("13800138000").userName("张三").build();
        User u2 = User.builder().userId("2").age(33).mobile("5234523").userName("李四").build();
        List<User> lst = Stream.of(u1, u2).collect(Collectors.toList());
        return lst;
    }
}
